package cn.marinda.resturants.service.impl;

import cn.marinda.resturants.entity.Salarys;
import cn.marinda.resturants.global.Pages;
import cn.marinda.resturants.mapper.SalarysMapper;
import cn.marinda.resturants.service.SalaryService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.UUID;

/** salarys 服务层实现类
 * @date 2021/12/2
 * @author Marinda
 */
@Service("salarysService")
public class SalarysServiceImpl extends ServiceImpl<SalarysMapper, Salarys> implements SalaryService {

    /**
     * 查询所有数据
     *
     * @return 工资列表
     */
    @Override
    public List<Salarys> queryList() {
        return baseMapper.selectListAll();
    }

    @Override
    public Salarys selectById(String uid) {
        return baseMapper.selectById(uid);
    }

    /**
     * 添加工资列
     *
     * @param salarys 工资
     * @return salarys
     */
    @Override
    public boolean addSalarys(Salarys salarys) {
        salarys.setUid(UUID.randomUUID().toString());
        System.out.println("salarys: " + salarys);
        return baseMapper.insert(salarys)>0;
    }

    /**
     * 删除工资列
     *
     * @param uid uid
     * @return 结果
     */
    @Override
    public boolean deleteSalarys(String uid) {
        return baseMapper.deleteById(uid)>0;
    }

    /**
     * 查询一个员工的基本工资
     *
     * @param uid 员工
     * @return 基本工资数据
     */
    @Override
    public BigDecimal selectBasic(String uid) {
        return baseMapper.selectBasic(uid);
    }

    /**
     * 查询一个员工的工时
     *
     * @param uid 员工号
     * @return 工时
     */
    @Override
    public Integer selectWorkDuration(String uid) {
        return baseMapper.selectWorkDuration(uid);
    }

    /**
     * 查询一个员工的水电费
     *
     * @param uid 员工号
     * @return 水电费
     */
    @Override
    public BigDecimal selectSundry(String uid) {
        return baseMapper.selectSundry(uid);
    }

    /**
     * 查询一个员工的全勤奖
     *
     * @param uid uid
     * @return 全勤奖
     */
    @Override
    public BigDecimal selectFillTime(String uid) {
        return  baseMapper.selectFillTime(uid);
    }

    /**
     * 查询一盒员工的请假次数
     *
     * @param uid uid
     * @return 请假次数
     */
    @Override
    public Integer selectLeave(String uid) {
        return baseMapper.selectLeave(uid);
    }

    /**
     * 查询应发工资
     *
     * @param uid uid
     * @return 应发工资
     */
    @Override
    public BigDecimal selectPayble(String uid) {
        return baseMapper.selectPayble(uid);
    }

    /**
     * 查询实发工资
     *
     * @param uid uid
     * @return 应发工资
     */
    @Override
    public BigDecimal selectNumber(String uid) {
        return baseMapper.selectNumber(uid);
    }

    /**
     * 乐观锁修改
     *
     * @param salarys 工资
     * @return 结果
     */
    @Override
    public boolean updateSalarys(Salarys salarys) {
        Salarys sqlSalarys = baseMapper.selectById(salarys.getUid());
        if(sqlSalarys == null){
            return false;
        }
        salarys.setVersion(sqlSalarys.getVersion());
        return lambdaUpdate().eq(Salarys::getUid,salarys.getUid()).eq(Salarys::getVersion,salarys.getVersion()).set(Salarys::getVersion,salarys.getVersion()+1).update(salarys);

    }

    /**
     * 分页查询
     *
     * @param page 分页
     * @return 分页
     */
    @Override
    public IPage<Salarys> selectPages(Pages<?> page) {
        return baseMapper.selectPages(page);
    }
}
